<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>long connection</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:9050/ljfirst");

        socket.onmessage = function (event) {
            var ta = document.getElementById("responsetext");
            ta.value = ta.value + "\n" + event.data;
        }

        socket.onopen = function (event) {
            var tt = document.getElementById("responsetext");
            tt.value = "连接开启";
        }

        socket.onclose = function (event) {
            var clt = document.getElementById("responsetext");
            clt.value = clt.value + "\n" + "连接断掉";
        }
    } else {
        alert("浏览器不支持websocket")
    }

    function send(msg) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(msg);
        }else {
            alert("连接尚未开启");
        }
    }

</script>


<form onsubmit="return false;">

    <!-- 此处的msg 关联上方的send(msg)-->
    <textarea name="msg" style="width: 400px; height: 200px"></textarea>
    <input type="button"  onclick="send(this.form.msg.value)" value="发送">

    <h3>服务端输出：</h3>
    <textarea id="responsetext" style="width: 400px; height: 200px"></textarea>
    <input type="button" onclick="javascript: document.getElementById('responsetext').value=''" value="清空内容">


</form>
</body>
</html>